package com.wxh.bomb.netty.thread.thread.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
	static ExecutorService ex = Executors.newFixedThreadPool(4);

	static class MyFile {
		private File file;
		// 用来表示当前文件的索引
		public AtomicInteger index;
		private ReentrantLock lock = new ReentrantLock();

		public MyFile(String filePath, int index) {
			file = new File(filePath);
			this.index = new AtomicInteger(index);
		}

		// 写入文件
		public void writeStr(String s) throws FileNotFoundException {
			FileOutputStream out = null;
			OutputStreamWriter cout = null;
			try {
				lock.lockInterruptibly();
				out = new FileOutputStream(file, true);
				cout = new OutputStreamWriter(out);
				cout.write(s);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					cout.close();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				lock.unlock();
			}
		}

		// 读取文件
		public String readStr() {
			FileInputStream in = null;
			InputStreamReader cin = null;
			BufferedReader br = null;
			// System.out.println("fawe");
			char[] cbuf = new char[100];
			try {
				in = new FileInputStream(file);
				cin = new InputStreamReader(in);
				br = new BufferedReader(cin);
				// br.readLine();
				StringBuilder sb = new StringBuilder();
				int temp = 0;
				while ((temp = br.read(cbuf)) > 0) {
					// System.out.println("fawe");
					sb.append(new String(cbuf), 0, temp);
					Arrays.fill(cbuf, ' ');
				}
				return sb.toString();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			} finally {
				try {
					in.close();
					cin.close();
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	static class WriteThread implements Runnable {
		private int flag;
		private MyFile[] myfiles;
		private int maxChars;

		private boolean isFinished = false;

		public WriteThread(int flag, MyFile[] myfiles, int maxChars) {
			this.flag = flag;
			this.myfiles = myfiles;
			this.maxChars = maxChars;
		}

		// @Override
		public void run() {
			int count = 0;
			while (count <= maxChars) {
				// System.out.println(count);
				// 遍历四个文件
				for (int i = 0; i < myfiles.length; i++) {
					// System.out.println(i+" "+myfiles[i].index.get());
					if ((myfiles[i].index.get() % 4) == flag) {

						try {
							myfiles[i].writeStr(String.valueOf(flag + 1));
							// System.out.println(myfiles[i].index.get());
							myfiles[i].index.incrementAndGet();
							count++;
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			// 线程已经写完
			isFinished = true;
		}

		public boolean isFinished() {
			return isFinished;
		}
	}

	static class ReadThread {
		MyFile[] myfiles = null;

		public ReadThread(MyFile[] myfiles) {
			this.myfiles = myfiles;
		}

		public void print() {
			for (int i = 0; i < myfiles.length; i++) {
				System.out.println(myfiles[i].readStr());
			}
		}
	}

	public static void main(String[] args) {
		// 建立文件
		MyFile[] myfiles = new MyFile[4];
		for (int i = 0; i < 4; i++) {
			myfiles[i] = new MyFile(
					"D:\\1111\\"
							+ (i + 1) + ".txt", i);
		}
		WriteThread t1 = new WriteThread(0, myfiles, 20);
		WriteThread t2 = new WriteThread(1, myfiles, 20);
		WriteThread t3 = new WriteThread(2, myfiles, 20);
		WriteThread t4 = new WriteThread(3, myfiles, 20);
		ex.execute(t1);
		ex.execute(t2);
		ex.execute(t3);
		ex.execute(t4);
		while (true) {
			// System.out.println("wait");
			if (t1.isFinished() && t2.isFinished() && t3.isFinished()
					&& t4.isFinished()) {
				ex.shutdown();
				// 读取文件
				new ReadThread(myfiles).print();
				break;
			}
		}
	}
}
